<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HexoWriter Pro</title>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.6.0/build/styles/atom-one-dark.min.css">
    <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.6.0/build/highlight.min.js"></script>

    <style>
        :root {
            --primary: #6C5CE7;
            --secondary: #A8A4FF;
            --background: #2D2D3A;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            background: var(--background);
            min-height: 100vh;
            color: #E4ECFA;
        }

        .container {
            max-width: 1600px;
            margin: 0 auto;
            padding: 2rem;
        }

        .header {
            text-align: center;
            margin-bottom: 2rem;
            position: relative;
        }

        .title {
            font-size: 2.5rem;
            background: linear-gradient(45deg, var(--primary), var(--secondary));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            letter-spacing: -0.05em;
            margin-bottom: 1rem;
        }

        .editor-wrapper {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 2rem;
            height: 80vh;
        }

        .editor-box, .preview-box {
            background: rgba(255, 255, 255, 0.05);
            border-radius: 12px;
            padding: 1.5rem;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        }

        #editor {
            width: 100%;
            height: 100%;
            background: transparent;
            border: none;
            resize: none;
            color: #E4ECFA;
            font-family: 'Fira Code', monospace;
            font-size: 14px;
            line-height: 1.6;
        }

        #preview {
            overflow-y: auto;
            height: 100%;
        }

        .header-btns {
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            display: flex;
            gap: 10px;
        }

        .header-btn {
            background: linear-gradient(45deg, var(--primary), var(--secondary));
            color: white;
            border: none;
            padding: 0.8rem 1.5rem;
            border-radius: 8px;
            cursor: pointer;
            transition: transform 0.2s;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .header-btn:hover {
            transform: scale(1.05);
        }

        /* Markdown 预览样式 */
        #preview h1, #preview h2, #preview h3 {
            color: #E4ECFA;
            margin: 1.5rem 0;
        }

        #preview a {
            color: var(--secondary);
            text-decoration: none;
        }

        #preview code {
            background: rgba(0, 0, 0, 0.3);
            padding: 0.2em 0.4em;
            border-radius: 4px;
        }

        #preview pre {
            background: rgba(0, 0, 0, 0.3);
            padding: 1rem;
            border-radius: 8px;
            overflow-x: auto;
            margin: 1rem 0;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1 class="title">HexoWriter Pro</h1>
        <div class="header-btns">
            <button class="header-btn" onclick="addHexoHeader()">
                <span>✨</span>添加头信息
            </button>
            <button class="header-btn" onclick="saveContent()">
                <span>💾</span>保存文档
            </button>
        </div>
    </div>

    <div class="editor-wrapper">
        <div class="editor-box">
            <textarea id="editor" placeholder="开始创作吧..."></textarea>
        </div>
        <div class="preview-box">
            <div id="preview"></div>
        </div>
    </div>
</div>

<script>
    // 配置marked
    marked.setOptions({
        highlight: function(code, lang) {
            return hljs.highlightAuto(code).value;
        },
        breaks: true,
        gfm: true
    });

    const editor = document.getElementById('editor');
    const preview = document.getElementById('preview');

    // Hexo Front-Matter模板
    const HEXO_HEADER = `---
title: {{标题}}
date: ${new Date().toISOString()}
tags:
  - 默认标签
categories: 默认分类
---

`;

    // 初始化内容
    editor.value = `# HexoWriter Pro
## 您的专业Hexo写作伴侣

\`\`\`javascript
// 试试代码块
const showcase = () => {
    console.log('高效写作体验');
}
\`\`\`

**核心功能：**
- 🎨 专业暗色主题
- ⚡ 实时双栏预览
- 📑 智能头信息管理
- 💾 一键持久化存储
`;

    // 实时预览
    function updatePreview() {
        preview.innerHTML = marked.parse(editor.value);
    }

    // 添加头信息
    function addHexoHeader() {
        const currentContent = editor.value;

        if (/^---\s*\n.*\n---\s*\n/is.test(currentContent)) {
            if (!confirm('检测到已有头信息，确定要替换吗？')) return;
            editor.value = currentContent.replace(/^---[\s\S]*?---\s*\n?/i, '');
        }

        editor.value = HEXO_HEADER.replace('{{标题}}', getDefaultTitle()) + editor.value;
        editor.focus();

        const titlePos = HEXO_HEADER.indexOf('{{标题}}') + 1;
        editor.setSelectionRange(titlePos, titlePos + 2);
    }

    // 生成默认标题
    function getDefaultTitle() {
        const date = new Date();
        return `新文章_${date.getFullYear()}${(date.getMonth()+1).toString().padStart(2, '0')}${date.getDate().toString().padStart(2, '0')}`;
    }

    // 保存内容（端口修改为9999）
    async function saveContent() {

        try {
            const response = await fetch('http://localhost:9999/save', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    content: editor.value,
                    filename: `doc_${Date.now()}.md`
                })
            });

            const result = await response.text();
            if (response.ok) {
                alert('保存成功: ' + result);
            } else {
                alert('保存失败: ' + result);
            }
        } catch (error) {
            alert('连接失败: ' + error.message);
        }
    }

    // 事件监听
    editor.addEventListener('input', updatePreview);
    updatePreview();
</script>
</body>
</html>